package com.malamute.system.action.system;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.malamute.core.action.BaseAction;
import com.malamute.core.exception.DatabaseException;
import com.malamute.system.entity.User;
import com.malamute.system.service.UserService;

/**
 * 管理员角色修改页面
 * 
 */
public class UserAdminAction extends BaseAction {
	@Autowired
	private UserService userService;

	private User admin;

	public User getAdmin() {
		return admin;
	}

	public void setAdmin(User admin) {
		this.admin = admin;
	}

	public String form() {
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.add(Restrictions.eq("username", "admin"));
		List list = userService.findUsersByUsername(criteria);
		if (list.isEmpty()) {
			throw new DatabaseException("admin is not found in database");
		} else {
			admin = (User) list.iterator().next();
		}
		return FORM;
	}

	public String update() {
		userService.updateUser(admin);
		return FORM;
	}
}
